using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThunderDownControl : MonoBehaviour
{
    private Animator anim;
    private string targetTag;
    private Vector3 createPosition;
    private int damage;

    public void Start()
    {
        anim = GetComponentInChildren<Animator>();
    }

    public void SetThunder(string targetTag,Vector3 targetPosition,int thunderDamage)
    { 
        this.targetTag = targetTag;
        this.createPosition = targetPosition;
        this.damage = thunderDamage;
        transform.position = createPosition + Vector3.up * 5;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == targetTag)
        {
            GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
            anim.SetTrigger("Strike");
            CharacterStats stats = null;
            if (stats = collision.gameObject.GetComponent<CharacterStats>())
            {
                stats.TakeDamage(damage);
            }
        }
        else if (collision.gameObject.tag == "Ground")
        {
            if (transform.position.y > createPosition.y)
            {
                return;
            }
        }
    }

    public void AnimationTrigger()
    {
        Destroy(gameObject);
    }
}
